package com.molinari.coding.activities.addconto;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.molinari.androidstructure.widget.arrayAdapter.ArrayAdapterCustom;
import com.molinari.coding.Cella;
import com.molinari.coding.R;

public class CelleAdapter extends ArrayAdapterCustom<Cella> {

	private Cella[] celle;
	
	public CelleAdapter(Context context, 
						Activity activity, 
						int resource,
						Cella[] objects) {
		
		super(context, activity, resource, objects);
		this.celle = objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = super.getView(position, convertView, parent);
		RowMomento memento = (RowMomento) convertView.getTag();
		
		int index1 = getPositionFirst(position);
		String valCellaUno = celle[index1].getUno() + celle[index1].getDue() + celle[index1].getTre();
		memento.uno.setText(valCellaUno);
		
		int index2 =  getPositionSecond(position);
		String valCellaDue = celle[index2].getUno() + celle[index2].getDue() + celle[index2].getTre();
		memento.due.setText(valCellaDue);
		return convertView;
	}
	
	private int getPositionFirst(int position){
		return (position+1)*2-2;
	}
	
	private int getPositionSecond(int position){
		return (position+1)*2-1;
	}
	
	@Override
	protected Object cercaViewMemento(View convertView, boolean viewEsistente, int position) {
		RowMomento memento = (RowMomento) super.cercaViewMemento(convertView, viewEsistente, position);
		int index1 = getPositionFirst(position);
		int index2 = getPositionSecond(position);
		
		memento.uno.setTag(celle[index1]);
		memento.due.setTag(celle[index2]);
		return memento;
	}

	@Override
	public void aggiornaViewMemento(Cella item, Object viewMemento, int position) {
		final RowMomento rowMomento = (RowMomento) viewMemento;
		
		
		int index1 = getPositionFirst(position);
		rowMomento.chiave1.setText(index1 + 1);
		rowMomento.uno.setText(celle[index1].getUno()+celle[index1].getDue()+celle[index1].getTre());
		
		int index2 = getPositionSecond(position);
		rowMomento.chiave2.setText(index2 +1);
		rowMomento.due.setText(celle[index2].getUno()+celle[index2].getDue()+celle[index2].getTre());
		
		
		int indexKey1 = Integer.parseInt(rowMomento.chiave1.getText().toString()) - 1;
		String unoTextMemo = rowMomento.uno.getText().toString();
		celle[indexKey1].setUno(unoTextMemo.substring(0,1));
		celle[indexKey1].setDue(unoTextMemo.substring(1,2));
		celle[indexKey1].setTre(unoTextMemo.substring(2,3));
		
		int indexKey2 = Integer.parseInt(rowMomento.chiave2.getText().toString()) - 1;
		String dueTextMemo = rowMomento.due.getText().toString();
		celle[indexKey2].setUno(dueTextMemo.substring(1,1));
		celle[indexKey2].setDue(dueTextMemo.substring(1,2));
		celle[indexKey2].setTre(dueTextMemo.substring(2,3));
			
		rowMomento.uno.addTextChangedListener(new CustomTextWatcher(rowMomento.uno, celle, item, 1));
		rowMomento.due.addTextChangedListener(new CustomTextWatcher(rowMomento.due, celle, item, 2));
	}

	@Override
	public Object initViewMemento(View convertView) {
		RowMomento rowMomento = new RowMomento();
		rowMomento.chiave1 = (TextView) convertView.findViewById(R.id.textNumber1);
		rowMomento.chiave2 = (TextView) convertView.findViewById(R.id.textNumber2);
		rowMomento.uno = (EditText) convertView.findViewById(R.id.editAggiungiPrimo);
		rowMomento.due = (EditText) convertView.findViewById(R.id.editAggiungiSecondo);
		
		return rowMomento;
	}
	
	class RowMomento {
		public TextView chiave1;
		public TextView chiave2;
		public EditText uno;
		public EditText due;
	}
	class CustomTextWatcher implements TextWatcher{
		
		EditText edit;
		Cella[] celle;
		Cella cella;
		int position;

		public CustomTextWatcher(EditText edit, Cella[] celle,Cella cella, int position) {
			this.edit = edit;
			this.celle = celle;
			this.cella = cella;
			this.position = position;
		}

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,int after) {
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,int count){ 
			Cella element=(Cella)edit.getTag();
			element.setUno(s.subSequence(0, 1).toString());
			element.setDue(s.subSequence(1, 2).toString());
			element.setTre(s.subSequence(2, 3).toString());
		}
	}

}
